//
//  MXOccoasionTopViewTableViewCell.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/6.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXOccoasionTopViewTableViewCell.h"
#import "MXOccoasionTopViewCollectionViewCell.h"
#import "MXCollocaStyleAndCationVC.h"
#import "AFNetworkHandler.h"
@interface MXOccoasionTopViewTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end


@implementation MXOccoasionTopViewTableViewCell
- (void)dealloc
{
    [_navi release];
    [_StyleLabel release];
    [_occoasionArray release];
    [_tempArray release];
    [_model release];
    [_OccocasionTitleLabel release];
    [_StyleTitleLabel release];
    [_collectionView release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self creatCollection];
        
        
        self.StyleTitleLabel = [[[UILabel alloc] init] autorelease];
        self.StyleTitleLabel.backgroundColor = [UIColor clearColor];
        self.StyleTitleLabel.text = @"风格";
        [self.collectionView addSubview:self.StyleTitleLabel];
        
        self.OccocasionTitleLabel = [[[UILabel alloc] init] autorelease];
        self.OccocasionTitleLabel.backgroundColor = [UIColor clearColor];
        self.OccocasionTitleLabel.text = @"场合";
        [self.collectionView addSubview:self.OccocasionTitleLabel];
        
        self.StyleLabel = [[[UILabel alloc] init] autorelease];
        self.StyleLabel.text = @"";
        self.StyleLabel.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.StyleLabel];
        
        
        self.tempArray = [NSMutableArray array];
        self.occoasionArray = [NSMutableArray array];
        
        
    }
    return self;
}



-(void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - HEIGHTPRO * 115);
    
    self.StyleTitleLabel.frame = CGRectMake(WIDTHPRO * 10, HEIGHTPRO * 10, WIDTHPRO * 60, HEIGHTPRO * 30);
    
    self.OccocasionTitleLabel.frame = CGRectMake( WIDTHPRO * 10, HEIGHTPRO * 250 + HEIGHTPRO * self.StyleTitleLabel.frame.origin.y, WIDTHPRO * 60, HEIGHTPRO * 30);
    
    self.StyleLabel.frame = CGRectMake(0, HEIGHT - HEIGHTPRO * 143, WIDTH, HEIGHTPRO * 30);
    
    UILabel *sumlabel = [[UILabel alloc] initWithFrame:CGRectMake( WIDTHPRO * 167, HEIGHTPRO * 5, WIDTHPRO * 40, HEIGHTPRO * 20)];
    sumlabel.backgroundColor = [UIColor clearColor];
    sumlabel.textAlignment = NSTextAlignmentCenter;
    sumlabel.text = @"精选";
    [sumlabel setTextColor:[UIColor grayColor]];
    [self.StyleLabel addSubview:sumlabel];
    
    UIView *line2 = [[[UIView alloc] initWithFrame:CGRectMake( WIDTHPRO * 30, HEIGHTPRO * 15,WIDTHPRO * 130, HEIGHTPRO * 1)] autorelease];
    line2.backgroundColor = [UIColor grayColor];
    [self.StyleLabel addSubview:line2];
    
    
    UIView *line3 = [[[UIView alloc] initWithFrame:CGRectMake(WIDTHPRO * 30 + self.frame.size.width / 2, HEIGHTPRO * 15, WIDTHPRO * 130, HEIGHTPRO * 1)] autorelease];
    line3.backgroundColor = [UIColor grayColor];
    [self.StyleLabel addSubview:line3];

    
}
- (void)creatCollection
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTHPRO * 60, HEIGHTPRO * 80);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTHPRO * 20 , HEIGHTPRO * 30 , 0, HEIGHTPRO * 10);
#pragma mark - 根据TopView手动更改
    flowLayout.headerReferenceSize = CGSizeMake(0, HEIGHTPRO * 20);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    
    [self.collectionView registerClass:[MXOccoasionTopViewCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

}

/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.tempArray.count;
    } else {
        return self.occoasionArray.count;
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return HEIGHTPRO * 50;
}

//重用机制

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXOccoasionTopViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.layer.masksToBounds = YES;
    if (indexPath.section == 0) {
        MXStyleModel *model = self.tempArray[indexPath.row];
        [cell GetInfo:model];
    } else if (indexPath.section == 1) {
        MXStyleModel *model = self.occoasionArray[indexPath.row];
        [cell GetInfo:model];

    }
        return cell;
}
//设置HeaderView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *topView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 10)];
    headerView.backgroundColor = [UIColor lightGrayColor];
    [topView addSubview:headerView];
    return topView;
}

#warning mark - 搭配界面风格 场合的CollectionView点击效果
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXCollocaStyleAndCationVC *styleAndCationVC = [[[MXCollocaStyleAndCationVC alloc] init] autorelease]; 
    if (indexPath.section == 0) {
        styleAndCationVC.titleString = [self.tempArray[indexPath.row] classify_name];
        MXStyleModel *model = self.tempArray[indexPath.row];
        styleAndCationVC.kindID = 1;
        styleAndCationVC.classifyID = model.classify_id;
    } else if (indexPath.section == 1) {
        styleAndCationVC.titleString = [self.occoasionArray[indexPath.row] classify_name];
        MXStyleModel *model = self.occoasionArray[indexPath.row];
        styleAndCationVC.kindID = 2;
        styleAndCationVC.classifyID = model.classify_id;

    }

    [self.navi presentViewController:styleAndCationVC animated:YES completion:^{
        
    }];
    if (indexPath.section == 0) {
    }

}
- (void)setTempArray:(NSMutableArray *)tempArray
{
    if (_tempArray != tempArray) {
        [_tempArray release];
        _tempArray = [tempArray retain];
    }
    [_collectionView reloadData];
}

- (void)setOccoasionArray:(NSMutableArray *)occoasionArray
{
    if (_occoasionArray != occoasionArray) {
        [_occoasionArray release];
        _occoasionArray = [occoasionArray retain];
    }
    [_collectionView reloadData];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
